﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Web.Mail;

/// <summary>
/// Summary description for Email
/// This is the class used to send the Email alert to a given/ selected registered user
/// Use two methods & smmtp server is smtp.gmail.com 
/// Can use in any application for any purpose 
/// Sends with our Authentication
/// Completely secured. Use SSL V3 & Https & a Encoded message
/// </summary>
public class clsGmail
{
    public clsGmail()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    /// <summary>
    /// This is the primery method try from the program
    /// Latest & easiest method of sending an Email using ASP.NET
    /// </summary>
    /// <param name="to"></param>
    /// <param name="from"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    //public static bool SendEmail(string to, string subject, string body, string attached)
    //{
    //    ///Smtp config
    //    SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["EmailServer"], 465);
    //    // Edit password and username
    //    client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["EmailUsername"], ConfigurationManager.AppSettings["EmailPassword"]);
    //    client.EnableSsl = true;

    //    ///mail details
    //    MailMessage msg = new MailMessage();


    //    try
    //    {

    //        msg.From = new MailAddress(ConfigurationManager.AppSettings["MasterEmail"]);
    //        msg.To.Add(to);
    //        // msg.SubjectEncoding = System.Text.Encoding.UTF8;
    //        msg.Subject = subject;
    //        //msg.CC.Add();
    //        msg.IsBodyHtml = true;
    //        msg.BodyEncoding = System.Text.Encoding.UTF8;
    //        msg.Body = body;
    //        msg.Priority = MailPriority.Normal;

    //        // Enable one of the following method.
    //        client.Send(msg);

    //        // or use the following alternative after enabling send mail asynchronous option in the global.asax 
    //        //object userState = msg;
    //        //client.SendAsync(msg, userState);
    //        return true;
    //    }
    //    catch (Exception exp)
    //    {
    //        ///This runs the backup plan
    //        //SendMailAlt(to, subject, body, attached);
    //        return false;
    //    }

    //}

    public static bool SendEmail(string to, string cc, string subject, string body, string attached)
    {
        try
        {
            MailMessage Mail = new MailMessage();
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = (ConfigurationManager.AppSettings["EmailServer"]);
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = ConfigurationManager.AppSettings["EmailPort"];
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            // Edit username & password
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = ConfigurationManager.AppSettings["EmailUsername"];
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = ConfigurationManager.AppSettings["EmailPassword"];

            Mail.To = to;
            if(cc.Length > 0)
                Mail.Bcc = cc;

            Mail.From = ConfigurationManager.AppSettings["EmailUsername"];
            Mail.Subject = subject;
            Mail.Body = body;
            Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            Mail.BodyEncoding = System.Text.Encoding.UTF8;

            System.Web.Mail.SmtpMail.SmtpServer = ConfigurationManager.AppSettings["EmailServer"];
            System.Web.Mail.SmtpMail.Send(Mail);

            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
}
